package demo2.category.api;

import com.fasterxml.jackson.databind.ObjectMapper;
import demo2.category.BookCategoryTree;

import java.io.IOException;
import java.io.OutputStream;

public class SaveJsonTextFileServiceImpl implements SaveService {
    private final OutputStream outStream;

    public SaveJsonTextFileServiceImpl(OutputStream outStream) {
        this.outStream = outStream;
    }

    @Override
    public void saveCategoryTree(BookCategoryTree tree) {
        ObjectMapper objectMapper = new ObjectMapper();
        try {
            objectMapper.writeValue(outStream, tree);
        } catch (IOException e) {
            throw new RuntimeException("Error: saveCategoryTree() failed to write IO", e);
        }
    }
}
